package com.example.administrator.zkdemo.fragment;

import android.annotation.SuppressLint;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.bigkoo.convenientbanner.ConvenientBanner;
import com.bigkoo.convenientbanner.holder.CBViewHolderCreator;
import com.bigkoo.convenientbanner.holder.Holder;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.example.administrator.zkdemo.activity.ZKSignInActivity;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.zkteco.android.model.ZKADPhotoBean;
import com.zkteco.android.utils.ZKFilePath;
import com.zkteco.android.utils.ZKFileUtils;
import com.zkteco.android.utils.ZKSPUtils;
import com.zkteco.android.utils.view.ZKProgressBarDialog;
import com.zkteco.android.utils.view.ZKProgressBarDialog.Builder;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import butterknife.BindView;

/**
 * Created with Android Studio.
 * 描述: ${DESCRIPTION}
 * Date: 2018/11/20
 * Time: 10:26
 *
 * @author 晏琦云
 * @version ${VERSION}
 */
public class ZKPhotoADFragment extends ZKBaseFragment {
    public static final String TAG = ZKPhotoADFragment.class.getCanonicalName();
    private int mAdTime;
    private ConvenientBanner mCBannerPhoto;
    private List<String> mImagePathList = new ArrayList();
    private AsyncTask<Void, Void, List<String>> mLoadAdPhotoTask;
    private ZKProgressBarDialog mProgressDialog;

    @BindView(R.id.rl_main)
    RelativeLayout rlMain;

    @SuppressLint({"StaticFieldLeak"})
    private void initData() {
        this.mAdTime = ((Integer) ZKSPUtils.getParam(getActivity(), "AD_TIME", Integer.valueOf(4))).intValue();
        this.mLoadAdPhotoTask = new AsyncTask<Void, Void, List<String>>() {
            protected List<String> doInBackground(Void[] paramArrayOfVoid) {
                ArrayList<String> localArrayList = new ArrayList();
                String str = (String) ZKSPUtils.getParam(ZKPhotoADFragment.this.getActivity(), "AD_PHOTO", "");
                if (str != null) {
                    if (!str.equals(""))
                        localArrayList = new Gson().fromJson(str, new TypeToken() {
                        }.getType());
                }
                ArrayList<File> localObject = new File(ZKFilePath.AD_PICTURE_PATH).listFiles();
                int j = localObject.size();
                int i = 0;
                while (i < j) {
                    File file = localObject.get(i);
                    if ((!file.isDirectory()) && (ZKFileUtils.checkPhotoSuffix(file.getName())) && (localObject.size() > 0)) {
                        Iterator localIterator = localArrayList.iterator();
                        while (localIterator.hasNext()) {
                            if (!((ZKADPhotoBean) localIterator.next()).getName().equals(file.getName()))
                                continue;
                            localArrayList.add(file.getAbsolutePath());
                        }
                    }
                    i += 1;
                }
                return localArrayList;
            }

            protected void onPostExecute(List<String> paramList) {
                super.onPostExecute(paramList);
                ZKPhotoADFragment.this.mProgressDialog.dismiss();
                ZKPhotoADFragment.this.mImagePathList.clear();
                ZKPhotoADFragment.this.mImagePathList.addAll(paramList);
                ZKPhotoADFragment.this.initView();
            }

            protected void onPreExecute() {
                super.onPreExecute();
                ZKPhotoADFragment.access$002(ZKPhotoADFragment.this, new ZKProgressBarDialog.Builder(ZKPhotoADFragment.this.getActivity()).setMessage(ZKPhotoADFragment.this.getResources().getString(2131624092)).create());
                ZKPhotoADFragment.this.mProgressDialog.show();
            }
        };
        this.mLoadAdPhotoTask.execute();
    }

    private void initView() {
        if (this.mCBannerPhoto != null) {
            this.mCBannerPhoto.setPages(new CBViewHolderCreator() {
                                            public Holder createHolder(View paramView) {
                                                return new ZKPhotoADFragment.ImageHolderView(ZKPhotoADFragment.this, paramView);
                                            }

                                            public int getLayoutId() {
                                                return R.layout.item_photo_ad;
                                            }
                                        }
                    , this.mImagePathList);
            this.mCBannerPhoto.startTurning(this.mAdTime * 1000);
        }
    }

    @Override
    protected int initLayout() {
        return R.layout.fragment_photo_ad;
    }

    public void onDestroy() {
        super.onDestroy();
        if (((this.mLoadAdPhotoTask != null) && (this.mLoadAdPhotoTask.getStatus() != AsyncTask.Status.FINISHED)) || (!this.mLoadAdPhotoTask.isCancelled()))
            this.mLoadAdPhotoTask.cancel(true);
        if ((this.mProgressDialog != null) && (this.mProgressDialog.isShowing()))
            this.mProgressDialog.dismiss();
    }

    public void onResume() {
        super.onResume();
    }

    public void onViewCreated(View paramView, @Nullable Bundle paramBundle) {
        super.onViewCreated(paramView, paramBundle);
        this.mCBannerPhoto = ((ConvenientBanner) paramView.findViewById(2131296393));
        initData();
    }

    private class ImageHolderView extends Holder<String> {
        private ImageView imgAd;

        public ImageHolderView(View arg2) {
            super();
        }

        protected void initView(View paramView) {
            this.imgAd = ((ImageView) paramView.findViewById(2131296435));
            this.imgAd.setOnClickListener(new View.OnClickListener() {
                public void onClick(View paramView) {
                    ((ZKSignInActivity) ZKPhotoADFragment.this.getActivity()).closeAD(ZKPhotoADFragment.this);
                }
            });
        }

        public void updateUI(String paramString) {
            RequestOptions options = new RequestOptions()
                    .centerCrop()
                    .skipMemoryCache(true)
                    .placeholder(2131230806)// 正在加载中的图片
                    .error(2131230806) // 加载失败的图片
                    .diskCacheStrategy(DiskCacheStrategy.NONE); // 磁盘缓存策略
            Glide.with(ZKPhotoADFragment.this.getActivity())
                    .load(paramString) // 图片地址
                    .apply(options) // 参数
                    .into(this.imgAd); // 需要显示的ImageView控件
        }
    }
}
